package com.lhkj.iams.wechat.config.ma;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.lhkj.iams.wechat.properties.WxMaProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
  * <p>
  *     描述：微信小程序配置
  * </p>
  */
@Configuration
@ConditionalOnClass({WxMaService.class,
//        WxPayService.class
})
@EnableConfigurationProperties(WxMaProperties.class)
public class WxMaConfiguration {

    @Resource
    private WxMaProperties wxMaProperties;

    /**
     * 小程序配置
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(WxMaProperties.class)
    public WxMaConfig wxMaConfig() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(this.wxMaProperties.getAppId());
        config.setSecret(this.wxMaProperties.getSecret());
        return config;
    }

    @Bean("wxMaService")
    @ConditionalOnMissingBean(WxMaProperties.class)
    public WxMaService wxMaService(WxMaConfig maConfig) {
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(maConfig);
        return service;
    }

//    /**
//     * 支付配置
//     *
//     * @return
//     */
//    @Bean
//    @ConditionalOnMissingBean(WxMaProperties.class)
//    public WxPayService wxService() {
//        WxPayConfig payConfig = new WxPayConfig();
//        payConfig.setAppId(StringUtils.trimToNull(this.wxMaProperties.getAppId()));
//        payConfig.setMchId(StringUtils.trimToNull(this.wxMaProperties.getMchId()));
//        payConfig.setMchKey(StringUtils.trimToNull(this.wxMaProperties.getMchKey()));
//        payConfig.setKeyPath(StringUtils.trimToNull(this.wxMaProperties.getKeyPath()));
//        payConfig.setTradeType(StringUtils.trimToNull(this.wxMaProperties.getTradeType()));
//        payConfig.setNotifyUrl(StringUtils.trimToNull(this.wxMaProperties.getNotifyUrl()));
//
//        // 可以指定是否使用沙箱环境
//        payConfig.setUseSandboxEnv(false);
//
//        WxPayService wxPayService = new WxPayServiceImpl();
//        wxPayService.setConfig(payConfig);
//        return wxPayService;
//    }
}
